package com.example.chapter08;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import androidx.appcompat.app.AppCompatActivity;

import com.example.chapter08.util.ToastUtil;

public class SpinnerDropdownActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    private final static String[] startArray = {"Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"};
    private Spinner spinner_dropdown;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinner_dropdown);

        spinner_dropdown = findViewById(R.id.spinner_dropdown);
        // Create an adapter for the spinner
        ArrayAdapter<String> startAdapter = new ArrayAdapter<>(this, R.layout.item_select, startArray);
        spinner_dropdown.setAdapter(startAdapter);
        // Set the default selection
        spinner_dropdown.setSelection(0);
        // Set the listener for the spinner, once the selection is changed, the onItemSelected method will be called
        spinner_dropdown.setOnItemSelectedListener(this);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        ToastUtil.show(this, "You selected: " + startArray[position]);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}